#!/bin/bash

# script/deploy.sh

echo "Starting Node.js application deployment..."
echo "Current directory: $(pwd)"
echo "User running script: $(whoami)"
echo "Date: $(date)"

# Add pnpm and pm2 to PATH
export PATH="/root/.deno/bin:/root/.local/share/pnpm:/root/.local/bin:/root/.nvm/versions/node/v22.11.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

# 切换到应用目录
echo "Changing to application directory..."
cd /root/new_taxengine/yunweibubackendnodejs

# 检查是否有本地修改
echo "Checking for local changes..."
git status

# 放弃本地修改
echo "Discarding local changes..."
git restore .

# 拉取最新代码
echo "Pulling latest code..."
git pull

# 安装依赖
echo "Installing dependencies..."
pnpm install

# 重启 PM2 应用
echo "Restarting PM2 application..."
pm2 restart yunweibu-dev-server --silent

echo "Deployment completed successfully."
exit 0


